// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from "axios";
import "lib-flexible"
import moment from 'moment'
import Vant from 'vant'
import 'vant/lib/index.css'
import * as dd from 'dingtalk-jsapi'
import VueWechatTitle from 'vue-wechat-title'
import "./assets/css/iconfont.css"
import global from './global'
Vue.prototype.Global = global;

Vue.use(Vant)
Vue.use(VueWechatTitle)
Vue.prototype.GLOBAL = global;
Vue.prototype.$http = axios;
Vue.prototype.$moment = moment
Vue.prototype.$dd = dd
Vue.config.productionTip = false

// import VConsole from "vconsole"
// const vConsole = new VConsole()

let token = localStorage.getItem('token')

// let expire = Number(localStorage.getItem("expire")) || 0;

dd.ready(function() {
  // 免登 // dd.ready参数为回调函数，在环境准备就绪时触发，jsapi的调用需要保证在该回调函数触发后调用，否则无效。
  dd.runtime.permission.requestAuthCode({
    corpId: global.corpId,
    onSuccess: function(result) {
      axios({
        url: '/api/auth/login',
        method: 'get',
        params: {
          corpId: global.corpId,
          code: result.code,
        }
      }).then(async res => {
        let resData = res.data;
        if (resData.errcode === 0) {
          let user = resData.data.user;
          let token = resData.data.token;
          let locatRoles = resData.data.user.locatRoles; //用户角色
          let userid = resData.data.user.userid; //用户ID
          localStorage.setItem("locatRoles", JSON.stringify(locatRoles));
          localStorage.setItem("userid", userid);
          localStorage.setItem("user", JSON.stringify(user));
          localStorage.setItem("token", token);

          new Vue({
            el: '#app',
            router,
            components: { App },
            template: '<App/>'
          })

        }
      }).catch(err => { console.log(err); })
    },

  });


  //添加全局请求头
  Vue.prototype.$http.interceptors.request.use(
    config => {
      config.headers.Authorization = localStorage.getItem("token");
      return config;
    },
    err => {
      return Promise.reject(err);
    }
  );

  // 钉钉组件注册
  let url = window.location.href.split("#")[0];
  axios.get(`/api/auth/signature?platform=${"mobile"}&url=${url}`).then(res => {
    let body = res.data;
    if (body.errcode === 0) {
      let resData = body.data;
      dd.config({
        agentId: resData.agentId, // 必填，微应用ID
        corpId: resData.corpId, //必填，企业ID
        timeStamp: resData.timeStamp, // 必填，生成签名的时间戳
        nonceStr: resData.nonceStr, // 必填，生成签名的随机串
        signature: resData.signature, // 必填，签名
        type: 0,
        jsApiList: [
            //需要使用的签名接口
            "runtime.info",
            "biz.contact.choose",
            "device.notification.confirm",
            "device.notification.alert",
            "device.notification.prompt",
            "biz.ding.post",
            "biz.util.openLink"
          ] // 必填，需要使用的jsapi列表，注意：不要带dd。
      });
    }
  });

});

//隐藏钉钉自带导航
// dd.biz.navigation.hideBar({
//   hidden: true,  // true：隐藏，false：显示
//   onSuccess: function (result) {
//   },
//   onFail: function (err) { }
// })

//设置导航标题
router.afterEach(route => {
  window.document.title = route.meta.title;
  localStorage.setItem('navName', route.meta.title);
  dd.biz.navigation.setTitle({
    title: route.meta.title, //控制标题文本，空字符串表示显示默认文本
    onSuccess: function(result) {
      /*结构
      {
      }*/
    },
    onFail: function(err) {}
  });


  // // 从路由的元信息中获取 title 属性
  // if (route.meta.title) {
  //   window.document.title = route.meta.title;
  //   localStorage.setItem('navName', route.meta.title);
  //   // 如果是 iOS 设备，则使用如下 hack 的写法实现页面标题的更新
  //   if (navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
  //     const hackIframe = document.createElement('iframe');
  //     hackIframe.style.display = 'none';
  //     hackIframe.src = '/static/html/fixIosTitle.html?r=' + Math.random();
  //     window.document.body.appendChild(hackIframe);
  //     setTimeout(_ => {
  //       window.document.body.removeChild(hackIframe)
  //     }, 300)
  //   }
  // }
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})